<template>
  <!-- "http://localhost:8443/api/covers" -->
  <!-- http://8.129.38.57:8443/api/covers 服务器远程图片上传地址-->
  <el-upload
    ref="upload"
    class="upload-covers"
    action="http://localhost:8443/api/covers"
    :on-preview="handlePreview"
    :on-remove="handleRemove"
    :before-remove="beforeRemove"
    :on-success="handleSuccess"
    multiple
    :limit="1"
    :on-exceed="handleExceed"
    :file-list="fileList"
  >
    <el-button size="small" type="primary">点击上传</el-button>
    <div slot="tip" class="el-upload__tip">
      只能上传jpg/png文件，且不超过500kb
    </div>
  </el-upload>
</template>

<script>
export default {
  name: 'ImgUpload',
  data () {
    return {
      fileList: [],
      url: ''
    }
  },
  methods: {
    handleRemove (file, fileList) {
      // console.log(file, fileList)
    },
    handlePreview (file) {
      // console.log(file)
    },
    // 文件超出限制时
    handleExceed (files, fileList) {
      this.$message.warning(
        `当前限制选择 1 个文件，本次选择了 ${files.length} 个文件，共选择了 ${
          files.length + fileList.length
        } 个文件`
      )
    },
    beforeRemove (file, fileList) {
      return this.$confirm(`确定移除 ${file.name}？`)
    },
    // 上传成功时
    handleSuccess (response) {
      console.log(response)
      this.url = response
      this.$emit('onUpload')
      this.$message.warning('上传成功!')
    },
    // 清除上传列表
    clear () {
      this.$refs.upload.clearFiles()
    }
  }
}
</script>
